package com.gusha.controller;

import java.math.BigInteger;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.gusha.config.SysOperateLog;
import com.gusha.dao.SysTrainingDao;
import com.gusha.model.InfoContent;
import com.gusha.model.Project;
import com.gusha.model.SysClassify;
import com.gusha.model.SysTraining;
import com.gusha.service.SysTrainingService;
import com.gusha.utils.Constants;
import com.gusha.utils.FileUtils;
import com.gusha.utils.JsonResult;
import com.gusha.utils.UpdateUtil;
import com.gusha.utils.WebValueUtil;

@Controller
@RequestMapping("/gx/training")
public class SysTrainingController {
	@Autowired
	private SysTrainingDao sysTrainingDao;
	
	@Autowired
	private SysTrainingService sysTrainingService;
	
    @RequestMapping("list")
    @SysOperateLog(operate = "查看培训列表")
    public String list(Model model, HttpServletRequest request) {

        //请求参数
        String pageIndex = WebValueUtil.getWebValue(request, "pageIndex");
        String trainingTitle = WebValueUtil.getWebValue(request, "trainingTitle");

        //参数封装
        int pageNo = 0;
        if (StringUtils.isNotBlank(pageIndex)) {
            pageNo = Integer.parseInt(pageIndex);
        }
        SysTraining sysTraining = new SysTraining();
        if (StringUtils.isNotBlank(trainingTitle)) {
        	sysTraining.setTrainingTitle(trainingTitle);
        }

        //获取分页数据
        Sort sort = new Sort(Sort.Direction.DESC, "addTime");
        Pageable pageable = PageRequest.of(pageNo, Constants.DEFAULT_PAGESIZE, sort);
        Page<SysTraining> page = sysTrainingService.findAll(pageable, sysTraining);
        model.addAttribute("page", page);

        model.addAttribute("trainingTitle", trainingTitle);
        return "/admin/sysTraining/list";
    }
    
    @RequestMapping("toAdd")
    @SysOperateLog(operate = "进入培训添加页面")
    public String toAdd(Model model) {
 
    
        return "/admin/sysTraining/form";
    }
    
    @RequestMapping("save")
    @ResponseBody
    @SysOperateLog(operate = "保存项目信息")
    public JsonResult save(SysTraining sysTraining,@RequestParam("fileName")MultipartFile file,HttpServletRequest request,@RequestParam("file1")MultipartFile file1) {
        JsonResult result = new JsonResult(false,"保存失败");
        String trainImgOld = WebValueUtil.getWebValue(request, "trainImgOld");
       
        if(!trainImgOld.equals(sysTraining.getTrainImg())) {
//       	 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
//            MultipartFile file1 = multipartRequest.getFile("file1");
        	
             // 要上传的目标文件存放路径
//            String newFileName = FileUtils.getFileName(file1.getOriginalFilename());
//            String imagePath = Constants.getFilePath("info")+newFileName;
//            String localPath = Constants.IMAGE_LOCAL_PATH+Constants.getFilePath("info");
//            FileUtils.upload(file, localPath, newFileName);
            
            String req= file1.getOriginalFilename();
            String imgPath = Constants.getFilePath("img")+req;
    		String localPath = Constants.IMAGE_LOCAL_PATH+Constants.getFilePath("img");
    		
    		
    		FileUtils.upload(file1, localPath, req);
            sysTraining.setTrainImg(imgPath);
      
       }
        if (sysTraining.getId() != null) {
        	SysTraining sysTrainingOld = sysTrainingDao.getOne(sysTraining.getId());
            UpdateUtil.copyNonNullProperties(sysTraining, sysTrainingOld);
        }
        
        String req= file.getOriginalFilename();
        String videoPath = Constants.getFilePath("video")+req;
		String localPath = Constants.IMAGE_LOCAL_PATH+Constants.getFilePath("video");
		
		
		FileUtils.upload(file, localPath, req);
		sysTraining.setTrainingVideo(videoPath);
        sysTrainingDao.save(sysTraining);
        result.setFlag(true);
        result.setMsg("保存成功");

        return result;
    }
}
